#include "tree.h"

void print_diff(CUString str1, CUString str2, FILE *fp )
{
	CUStringList ls1;
	ls1.TokenizeByChar( str1, " ", false );

	CUStringList ls2;
	ls2.TokenizeByChar( str2, " ", false );

	bool is_first = true;
//	fprintf( fp,"\n\n%s\n%s\n\n", str1.GetStr(), str2.GetStr() );
	for( int i=0; i<ls1.GetSize(); i++ )	{
		CUString tok1 = ls1.GetAt(i);

		bool found = false;
		for( int j=0; j<ls2.GetSize(); j++ )	{
			CUString tok2 = ls2.GetAt(j);
			if( tok1 == tok2 )	{
				found = true;
				break;
			}
		}
		if( found == false )	{
			if( is_first == true )	{
				is_first = false;
			}
			else	{
				fprintf( fp, " " );
			}


			fprintf( fp, "%s", tok1.GetStr() ) ;
		}
	}

//	fprintf( fp, "---- " );
	for( int i=0; i<ls2.GetSize(); i++ )	{
		CUString tok2 = ls2.GetAt(i);

		bool found = false;
		for( int j=0; j<ls1.GetSize(); j++ )	{
			CUString tok1 = ls1.GetAt(j);
			if( tok1 == tok2 )	{
				found = true;
				break;
			}
		}
		if( found == false )	{
			if( is_first == true )	{
				is_first = false;
			}
			else	{
				fprintf( fp, " " );
			}


			fprintf( fp, "%s", tok2.GetStr() ) ;
		}
	}
	fprintf( fp,"\n" );

//	fprintf( fp,"\n\n" );
}

bool start( char *gui1_fn, char *gui2_fn, char *out_fn )
{
	CUTextFile gui1_file( gui1_fn, "r" );
	if( gui1_file.IsOpen() == false )	return false;

	CUTextFile gui2_file( gui2_fn, "r" );
	if( gui2_file.IsOpen() == false )	return false;

	CUTextFile out_file( out_fn, "w" );
	if( out_file.IsOpen() == false )	return false;

	int now_line=0;
	while(1)	{
		CUString gui1_line;
		CUString gui2_line;

		// src org
		now_line++;
		if( gui1_file.ReadLine( gui1_line ) == false ) break;
		if( gui2_file.ReadLine( gui2_line ) == false ) break;
		if( gui1_line != gui2_line )	{
			fprintf( stderr, "diff org\n" );
			return false;
		}
		fprintf( out_file.GetFP(), "%s\n", gui1_line.GetStr() );


		// tgt org
		now_line++;
		if( gui1_file.ReadLine( gui1_line ) == false ) break;
		if( gui2_file.ReadLine( gui2_line ) == false ) break;
		if( gui1_line != gui2_line )	{
			fprintf( stderr, "diff tgt\n" );
			return false;
		}
		fprintf( out_file.GetFP(), "%s\n", gui1_line.GetStr() );


		// src word
		now_line++;
		if( gui1_file.ReadLine( gui1_line ) == false ) break;
		if( gui2_file.ReadLine( gui2_line ) == false ) break;
		if( gui1_line != gui2_line )	{
			fprintf( stderr, "diff src word\n" );
			return false;
		}
		fprintf( out_file.GetFP(), "%s\n", gui1_line.GetStr() );

		// tgt word
		now_line++;
		if( gui1_file.ReadLine( gui1_line ) == false ) break;
		if( gui2_file.ReadLine( gui2_line ) == false ) break;
		if( gui1_line != gui2_line )	{
			fprintf( stderr, "diff tgt word\n" );
			return false;
		}
		fprintf( out_file.GetFP(), "%s\n", gui1_line.GetStr() );

		// wa
		now_line++;
		if( gui1_file.ReadLine( gui1_line ) == false ) break;
		if( gui2_file.ReadLine( gui2_line ) == false ) break;
		/*
		if( gui1_line != gui2_line )	{
			fprintf( stderr, "diff wa\n" );
			return false;
		}
		*/
		print_diff( gui1_line, gui2_line, out_file.GetFP() );

		// src phrase
		now_line++;
		if( gui1_file.ReadLine( gui1_line ) == false ) break;
		if( gui2_file.ReadLine( gui2_line ) == false ) break;
		if( gui1_line != gui2_line )	{
			fprintf( stderr, "diff src word\n" );
			return false;
		}
		fprintf( out_file.GetFP(), "%s\n", gui1_line.GetStr() );

		// tgt phrase
		now_line++;
		if( gui1_file.ReadLine( gui1_line ) == false ) break;
		if( gui2_file.ReadLine( gui2_line ) == false ) break;
		if( gui1_line != gui2_line )	{
			fprintf( stderr, "diff tgt word\n" );
			return false;
		}
		fprintf( out_file.GetFP(), "%s\n", gui1_line.GetStr() );

		// phrase align
		now_line++;
		if( gui1_file.ReadLine( gui1_line ) == false ) break;
		if( gui2_file.ReadLine( gui2_line ) == false ) break;
		if( gui1_line != gui2_line )	{
			fprintf( stderr, "diff phrase align\n" );
			return false;
		}
		fprintf( out_file.GetFP(), "%s\n", gui1_line.GetStr() );

	}

	fprintf( stdout, "%d lines\n", now_line );


	return true;
}


int main( int argc, char *argv[] )
{
	if( argc != 4 )	{
		fprintf( stderr, "usage: guidiff gui1 gui2 out\n" );
		fprintf( stderr, "output: union of gui1-gui2 and gui2-gui1, to see disagreed word alignments\n" );
		return 1;
	}

	if( start( argv[1], argv[2], argv[3] ) == false )	{
		return 1;
	}

	return 0;
}


